<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/6/11
 * Time: 16:03
 */

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use yii\web\Session;
use yii\redis\get;
use \common\helps\tools;//使用工具类

/**
 * Login controller
 */
class LoginController extends Controller
{

    // 视图类实例
    protected $view;
    // token
    protected $token;

    public $user = [];
    /**
     * @var 令牌盐值
     */
    protected $accessTokenStr = 'heikezhidao';


    /**
     * 行为
     * {@inheritdoc}
     */
//    public function behaviors()
//    {
//        return [
//            'access' => [
//                'class' => AccessControl::className(),
//                'rules' => [
//                    [
//                        'actions' => ['login', 'error'],
//                        'allow' => true,
//                    ],
//                    [
//                        'actions' => ['logout', 'index'],
//                        'allow' => true,
//                        'roles' => ['@'],
//                    ],
//                ],
//            ],
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'logout' => ['post'],
//                ],
//            ],
//        ];
//    }


    //禁用yii2的csrf验证
//    public $enableCsrfValidation = false;
    public function init()
    {
        $this->enableCsrfValidation = false;
    }


    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * title 登录
     * @return string
     */
    public function actionLogin()
    {
        return $this->render('login');
    }

    /**
     * title 选择身份
     * @return string
     */
    public function actionChoicerole()
    {
        return $this->render('choicerole');
    }


    /**
     * title  登录验证
     * @return string
     */
    public function actionChecklogin()
    {
        header('Access-Control-Allow-Origin:*');
        $post = Yii::$app->request->post();
        $user_name = $post['user_name'];
        $pwd = $post['pwd'];
        $query = new \yii\db\Query();
        //是否存在用户
        $this->user = $query->select('*')->from('data_yii.d_user')->where(['phone' =>$user_name ])->one();
        if(!$this->user){
            return tools::ajax_return_error('2','没有此用户名',[]);
        }

        //密码是否正确
        if($this->user['password'] != MD5($pwd)){
            return tools::ajax_return_error('2','密码不正确',[]);
        }

        //token
        $this->token = sha1($this->user['username'].$this->user['password'].$this->accessTokenStr).time();

        $this->user['token'] = $this->token;

        //权限
        $role =  $query->select(['id','role_id'])->from('data_yii.d_role_user')->where(['user_id' =>$this->user['id'] ])->all();
        $this->user['role'] = $role;

        //存储用户信息
        $redis = Yii::$app->cache;
        $redis->set('admin_'.$this->user['id'],$this->user);

        return tools::ajax_return('0','成功',$this->user);





    }






}